package com.hsq.androidproject.utils;

import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2016/11/19.
 */

public class SDUtils {
    //判断SD卡是否存在
    public static boolean isSDExists() {
        //根据是否可以读写
        //返回true 则可读 可写  否则 sd卡不存在
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return true;
        }
        //默认SD卡不存在
        return false;
    }

    public static String getPath() {
        if (isSDExists()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return null;
    }


    //将数据存储到SD卡中

    /**
     * @param data     向存储的数据
     * @param dir      向存储到的目录
     * @param fileName 存储文件的名字
     * @return 是否存储成功
     */
    public static boolean saveToSd(byte[] data, String dir, String fileName) {
        if (isSDExists()) {
            String path = getPath() + File.separator + dir;
            File file = new File(path);
            if (!file.exists()) {
                //如果文件夹不存在 则创建
                file.mkdir();
            }
            BufferedOutputStream bos = null;
            try {
                //带缓冲的输出流
                bos = new BufferedOutputStream(new FileOutputStream(new File(path, fileName)));
                bos.write(data, 0, data.length);
                //刷新
                bos.flush();
                return true;

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
        return false;
    }


    /**
     * 从SD卡中读取数据
     *
     * @param url 地址
     * @return
     */
    public static byte[] getDataFromSD(String url) {
        if (isSDExists()) {/*
            String path = getPath()
                    + File.separator + url + File.separator + fileName;
    */
            String path = url;

            File file = new File(path);

            if (file.exists()) {
                //带缓冲的输入流
                BufferedInputStream bis = null;
                //从内存输出到手机中
                ByteArrayOutputStream baos = null;
                try {
                    baos = new ByteArrayOutputStream();
                    bis = new BufferedInputStream(new FileInputStream(file));

                    int len = 0;
                    byte[] buffer = new byte[1024 * 4];
                    while ((len = bis.read(buffer)) != -1) {
                        baos.write(buffer, 0, len);
                        baos.flush();
                    }
                    return baos.toByteArray();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
